package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.MainActivity;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.MoreCityAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.ActivityManager;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.CityBean;
import cn.com.ddapp.ddm.bean.ShiQuBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TLog;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/9/1 0001.
 */
public class CityMoreListActivity extends BaseActivity {
    @BindView(R.id.more_list)
    ListView moreList;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.img_error_layout)
    ImageView img;
    private String id;
    private ShiQuBean shiquBean;

    @Override
    public void initView() {
        setTitle("市区列表");
        setTitleLeftImg(R.mipmap.back_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
    }

    @Override
    public void initData() {
        id = getIntent().getExtras().getString("id");
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(CityMoreListActivity.this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
//                    TLog.log(getFragmentTag(), response);
//                    Log.e("TAG", "onMyResponsesponse: " + response);
                    shiquBean = gson.fromJson(response, ShiQuBean.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(shiquBean.getResult())) && shiquBean.getLogin()) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(shiquBean.getResult()))) {
                        BaseApplication.getIntstance().showToast(shiquBean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if (shiquBean.getResult().equals("OK") && shiquBean.getAddrGroup().size() == 0) {
                        mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(shiquBean.getResult()))) {
                        cityList();//内容视频
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            };
            OKUtilAPI.post(OKUtilAPI.CITYLIST,"parentId="+id, okUtilCallback);
        }

    }

    private void cityList() {
        List<ShiQuBean.AddrGroupBean> addrGroup = shiquBean.getAddrGroup();
        MoreCityAdapter moreCityAdapter = new MoreCityAdapter(this, addrGroup);
        moreList.setAdapter(moreCityAdapter);
        moreList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                AppConfig.CITY_ID=shiquBean.getAddrGroup().get(position).getId();
                System.out.println("AppConfig.CITY_ID=="+AppConfig.CITY_ID);
                AppConfig.WID=shiquBean.getAddrGroup().get(position).getId();
                Log.i("aaa","--aid--"+AppConfig.CITY_ID);
                AppConfig.City_NAME=shiquBean.getAddrGroup().get(position).getName();
                SPUtils.setStringData(CityMoreListActivity.this,SPUtils.AREANAME,shiquBean.getAddrGroup().get(position).getName());
                Intent intent=new Intent(CityMoreListActivity.this, MainActivity.class);
                intent.putExtra("id", 2);
                startActivity(intent);
                ActivityManager.getInstance().finishActivity(CitysListActivity.class);
                finish();
                AppConfig.ATMFVISIT="2";
                AppConfig.APPFVISIT="2";
                AppConfig.FIN="2";

            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_more_list;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }
}
